package com.show.service.population;

import com.show.dao.population.PopulationRepository;
import com.show.dto.population.Population;
import com.show.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 人口service
 *
 * @author 姜文
 */
@Service
public class PopulationService {

    private QueryUtil queryUtil;

    private PopulationRepository populationRepository;

    @Autowired
    public PopulationService(QueryUtil queryUtil, PopulationRepository populationRepository) {
        this.queryUtil = queryUtil;
        this.populationRepository = populationRepository;
    }


    /**
     * 大屏障人口展示
     *
     * @author 姜文
     */
    public List<Population> findPopulation() {
        String sql = "SELECT\n" +
                "\tCOUNT(*) AS countPopulation,\n" +
                "\tbs.base_station_name AS baseStationName\n" +
                "FROM\n" +
                "\t`population` AS p\n" +
                "LEFT JOIN base_station AS bs ON p.base_station_id = bs.base_station_id\n" +
                "WHERE\n" +
                "\tp.cdate BETWEEN date_sub(NOW(), INTERVAL 1 HOUR)\n" +
                "AND NOW()\n" +
                "GROUP BY\n" +
                "\tp.base_station_id";
        List<Population> populationList = queryUtil.query(sql, Population.class);
        return populationList;
    }

    /**
     * 热力图展示
     *
     * @author 姜文
     */
    public List<Population> getPopulation() {
        String sqlCount = "SELECT COUNT(*) AS countPopulation FROM `population`";
        List<Population> populationCount = queryUtil.query(sqlCount, Population.class);
        String count = populationCount.get(0).getCountPopulation();
        String sql = "SELECT * FROM `population`";
        List<Population> populationList = queryUtil.query(sql, Population.class);
        for(Population p : populationList){ p.setCountPopulation(count); }
        return populationList;
    }

    /**
     * 基站热力图展示
     *
     * @author 姜文
     */
    public List<Population> getPopulationForJZ(String id) {
        String sqlCount = "SELECT COUNT(*) AS countPopulation FROM `population` WHERE base_station_id = '"+id+"'";
        List<Population> populationCount = queryUtil.query(sqlCount, Population.class);
        String count = populationCount.get(0).getCountPopulation();
        String sql = "SELECT * FROM `population` WHERE base_station_id = '"+id+"'";
        List<Population> populationList = queryUtil.query(sql, Population.class);
        for(Population p : populationList){ p.setCountPopulation(count); }
        return populationList;
    }

}
